package itsm.isperp.module.service.db;

import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.service.IDatalogService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.module.entity.db.DbEntityDatalog;
import itsm.isperp.module.repository.db.DbEntityDatalogMapper;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * 数据日志
 * 
 * @author lizx
 * @date 2014-5-14
 * @since 1.0
 * 
 */
@Service
public class DbEntityDatalogService extends BaseService<DbEntityDatalog>
		implements IDatalogService<DbEntityDatalog> {

	@Autowired
	protected DbEntityDatalogMapper dbEntityDatalogMapper;

	@Override
	public DbEntityDatalogMapper getRepository() {
		return dbEntityDatalogMapper;
	}

	@Override
	public int save(DbEntityDatalog entity) {
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setId(HibernateUUIDGenerator.generate());
		}

		return this.dbEntityDatalogMapper.save(entity);
	}

	@Override
	public void deleteByBusinessId(String businessId) {
		this.getRepository().deleteByBusinessId(businessId);
	}

	@Override
	public List<DbEntityDatalog> findByBusinessId(String businessId) {
		return this.getRepository().findByBusinessId(businessId);
	}

	public void updateChangeId(String changeId, String id) {
		this.dbEntityDatalogMapper.updateChangeId(changeId, id);
	}

	public DbEntityDatalog findLastByBusinessId(String businessId) {
        if(this.dbEntityDatalogMapper.findLastByBusinessId(businessId).size()==0)
               return null;
        else
        	   return this.dbEntityDatalogMapper.findLastByBusinessId(businessId).get(0);
	}

	
}
